package sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@Tag(name = "C端-购物车接口", description = "购物车相关业务")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @Operation(summary = "添加购物车", description = "添加购物车")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "成功添加购物车")
    )
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：{}", shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    @Operation(summary = "查看购物车", description = "查看购物车")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "成功查看购物车")
    )
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
        return Result.success(list);
    }


    @Operation(summary = "清空购物车", description = "清空购物车")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "成功清空购物车")
    )
    @DeleteMapping("/clean")
    public Result clean() {
        shoppingCartService.clean();
        return Result.success();
    }

    @Operation(summary = "购物车商品数量减少", description = "购物车商品数量减少")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "成功购物车商品数量减少")
    )
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
    	log.info("购物车减少商品：{}", shoppingCartDTO);
    	shoppingCartService.sub(shoppingCartDTO);
    	return Result.success();
    }
}
